/**
 * @param {number} x
 * @return {number}
 */
var mySqrt = function (x) {

  // 首先判断边界值
  // 0和1的平方根是它本身
  if (x < 2) {
    return x
  }
  var lo = 0, hi = x, mid;
  while (lo <= hi) {
    mid = Math.ceil((lo + hi) / 2)
    if (mid * mid == x) return mid;
    if (mid * mid < x) {
      lo = mid + 1
    } else {
      hi = mid - 1;
    }
  }
  return Math.floor(hi);

};
console.log(mySqrt(9))


